﻿namespace Pub.Class
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    public class Request2
    {
        public static string Get(string VarName)
        {
            string str = string.Empty;
            if (HttpContext.Current.Request[VarName].IsNotNull())
            {
                str = HttpContext.Current.Request[VarName].ToString();
            }
            return str;
        }

        public static string GetBrowser()
        {
            HttpBrowserCapabilities capabilities = new HttpBrowserCapabilities();
            return HttpContext.Current.Request.Browser.Type;
        }

        public static string GetCurrentFullHost()
        {
            HttpRequest request = HttpContext.Current.Request;
            if (!request.Url.IsDefaultPort)
            {
                return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
            }
            return request.Url.Host;
        }

        public static string GetF(string VarName)
        {
            string str = string.Empty;
            if (HttpContext.Current.Request.Form[VarName].IsNotNull())
            {
                str = HttpContext.Current.Request.Form[VarName].ToString();
            }
            return str;
        }

        public static float GetFFloat(string varName, int defValue)
        {
            return GetF(varName).ToFloat(((float) defValue));
        }

        public static int GetFInt(string varName, int defValue)
        {
            return GetF(varName).ToInt(defValue);
        }

        public static float GetFloat(string varName, int defValue)
        {
            return Get(varName).ToFloat(((float) defValue));
        }

        public static long GetFLong(string varName, long defValue)
        {
            return GetF(varName).ToBigInt(defValue);
        }

        public static int GetFParamCount()
        {
            return HttpContext.Current.Request.Form.Count;
        }

        public static string GetHost()
        {
            return HttpContext.Current.Request.Url.Host;
        }

        public static int GetInt(string varName, int defValue)
        {
            return Get(varName).ToInt(defValue);
        }

        public static string GetIP()
        {
            if (HttpContext.Current.IsNull())
            {
                return string.Empty;
            }
            string input = string.Empty;
            input = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if ((input != null) && (input != string.Empty))
            {
                if (input.IndexOf(".") == -1)
                {
                    input = null;
                }
                else if (input.IndexOf(",") != -1)
                {
                    input = input.Replace(" ", "").Replace("'", "");
                    string[] strArray = input.Split(",;".ToCharArray());
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if ((strArray[i].IsIP() && (strArray[i].Substring(0, 3) != "10.")) && ((strArray[i].Substring(0, 7) != "192.168") && (strArray[i].Substring(0, 7) != "172.16.")))
                        {
                            return strArray[i];
                        }
                    }
                }
                else
                {
                    if (input.IsIP())
                    {
                        return input;
                    }
                    input = null;
                }
            }
            if (!HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].IsNotNull() || (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == string.Empty))
            {
                string text1 = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                string text2 = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            if (input == null)
            {
                input = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (input == null)
            {
                input = HttpContext.Current.Request.UserHostAddress;
            }
            return input;
        }

        public static string GetOS()
        {
            HttpBrowserCapabilities capabilities = new HttpBrowserCapabilities();
            return HttpContext.Current.Request.Browser.Platform;
        }

        public static string GetPageName()
        {
            string[] strArray = HttpContext.Current.Request.Url.AbsolutePath.Split(new char[] { '/' });
            return strArray[strArray.Length - 1].ToLower();
        }

        public static int GetParamCount()
        {
            return (HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count);
        }

        public static string GetQ(string VarName)
        {
            string str = string.Empty;
            if (HttpContext.Current.Request.QueryString[VarName].IsNotNull())
            {
                str = HttpContext.Current.Request.QueryString[VarName].ToString();
            }
            return str;
        }

        public static float GetQFloat(string varName, int defValue)
        {
            return GetQ(varName).ToFloat(((float) defValue));
        }

        public static int GetQInt(string varName, int defValue)
        {
            return GetQ(varName).ToInt(defValue);
        }

        public static long GetQLong(string varName, long defValue)
        {
            return GetQ(varName).ToBigInt(defValue);
        }

        public static int GetQParamCount()
        {
            return HttpContext.Current.Request.QueryString.Count;
        }

        public static string GetRawUrl()
        {
            return HttpContext.Current.Request.RawUrl;
        }

        public static string GetReferrer()
        {
            string str = GetServerString("HTTP_REFERER").Trim();
            if (str == null)
            {
                str = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            if (str == null)
            {
                return string.Empty;
            }
            return str;
        }

        public static string GetRelativeRoot()
        {
            return "~/".GetAbsolutePath();
        }

        public static string GetRequest()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("form:");
            for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
            {
                sb.Append(HttpContext.Current.Request.Form.Keys[i].ToString() + "=" + HttpContext.Current.Request.Form[i].ToString() + "&");
            }
            sb.RemoveLastChar("&");
            sb.Append("querystring:");
            for (int j = 0; j < HttpContext.Current.Request.QueryString.Count; j++)
            {
                sb.Append(HttpContext.Current.Request.QueryString.Keys[j].ToString() + "=" + HttpContext.Current.Request.QueryString[j].ToString() + "&");
            }
            sb.RemoveLastChar("&");
            return sb.ToString();
        }

        public static string GetRootUrl(string forumPath)
        {
            int port = HttpContext.Current.Request.Url.Port;
            return string.Format("{0}://{1}{2}{3}", new object[] { HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host.ToString(), ((port == 80) || (port == 0)) ? "" : (":" + port), forumPath });
        }

        public static string GetServerString(string strName)
        {
            if (HttpContext.Current.Request.ServerVariables[strName].IsNull())
            {
                return string.Empty;
            }
            return HttpContext.Current.Request.ServerVariables[strName].ToString();
        }

        public static string GetUrl()
        {
            return HttpContext.Current.Request.Url.ToString();
        }

        public static bool IsBrowserGet()
        {
            string[] strArray = new string[] { "ie", "opera", "netscape", "mozilla", "konqueror", "firefox" };
            string str = HttpContext.Current.Request.Browser.Type.ToLower();
            for (int i = 0; i < strArray.Length; i++)
            {
                if (str.IndexOf(strArray[i]) >= 0)
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsGet()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("GET");
        }

        public static bool IsLinux()
        {
            int platform = (int) Environment.OSVersion.Platform;
            if (platform != 4)
            {
                return (platform == 0x80);
            }
            return true;
        }

        public static bool IsMobile()
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request.IsNull())
            {
                return false;
            }
            return (request.Browser.IsMobileDevice || (!string.IsNullOrEmpty(request.UserAgent) && new Regex(@"(iemobile|iphone|ipod|android|nokia|sonyericsson|blackberry|samsung|sec\-|windows ce|motorola|mot\-|up.b|midp\-)", RegexOptions.Compiled | RegexOptions.IgnoreCase).IsMatch(request.UserAgent)));
        }

        public static bool IsMono()
        {
            return Type.GetType("Mono.Runtime").IsNotNull();
        }

        public static bool IsPost()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("POST");
        }

        public static bool IsPostFile()
        {
            for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
            {
                if (HttpContext.Current.Request.Files[i].FileName != "")
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsSearchEnginesGet()
        {
            if (HttpContext.Current.Request.UrlReferrer.IsNotNull())
            {
                string[] strArray = new string[] { "google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom", "yisou", "iask", "soso", "gougou", "zhongsou" };
                string str = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (str.IndexOf(strArray[i]) >= 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static void SaveRequestFile(string path)
        {
            if (HttpContext.Current.Request.Files.Count > 0)
            {
                HttpContext.Current.Request.Files[0].SaveAs(path);
            }
        }
    }
}

